package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        try (Connection connection=DBUtil.getConnection()){
            Statement statement=connection.createStatement();
            String sql="SELECT c.name,c.floor,t.name,t.salary,t.title " +
                    "FROM class c " +
                    "JOIN teacher t ON c.teacher_id=t.id";
            ResultSet rs=statement.executeQuery(sql);
            while(rs.next()){
                //也可以通过指定字段下标获取，但是数据库的下标从1开始
                String cName=rs.getString(1);
                int cFloor=rs.getInt(2);
                String tName=rs.getString(3);
                int tSalary=rs.getInt(4);
                String tTitle=rs.getString(5);
                System.out.println(cName+" "+cFloor+" "+tName+" "+tSalary+" "+tTitle);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
